<template>
  <div>
    <el-card class="main-card">
      <el-row>
        <el-col :span="9">
          <el-input
            placeholder="请输入套餐名称/编码"
            v-model="queryInfo.queryString"
            @clear="findPage"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="findPage"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table
        :data="tableList"
        stripe
        border
        highlight-current-row
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        max-height="450"
      >
        <el-table-column type="index" label="序号" width="80">
        </el-table-column>
        <el-table-column prop="code" label="套餐编码" width="180">
        </el-table-column>
        <el-table-column prop="name" label="套餐名称" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="helpCode" label="助记码"> </el-table-column>
        <el-table-column prop="sex" label="适用性别" :formatter="formatSex">
        </el-table-column>
        <el-table-column prop="age" label="适用年龄"> </el-table-column>
        <el-table-column prop="remark" label="套餐说明" show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          prop="attention"
          label="套餐注意事项"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column label="套餐图片" width="120" prop="img">
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="
                scope.row.img.startsWith('https')
                  ? scope.row.img
                  : $qiniu + scope.row.img
              "
              :preview-src-list="preview(scope.row.img)"
            >
            </el-image>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="120">
          <template slot-scope="scope">
            <el-button type="primary" @click="findById(scope.row.id)"
              >详情查看</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="handlePageSize"
          @current-change="handlePageNumber"
          background
          :current-page="queryInfo.pageNumber"
          :page-sizes="[8, 16, 32, 64, 128]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "order",
  data() {
    return {
      // 分页查询条件
      queryInfo: {
        // 页码
        pageNumber: 1,
        // 页数
        pageSize: 8,
        // 关键字
        queryString: null,
      },
      // 表格数据
      tableList: [],
      // 总记录数
      total: 0,
      // 表单标题
      title: null,
      // 是否打开对话框
      // 表格加载
      loading: false,
      open: false,
      // 表单数据
      form: {},
    };
  },
  /* 页面初始化调用方法 */
  created() {
    this.findPage();
  },
  methods: {
    /** 分页查询 */
    findPage() {
      this.loading = true;
      this.$ajax.post("/setmeal/findPage", this.queryInfo).then((res) => {
        this.loading = false;
        console.log("res.data", res.data);
        this.tableList = res.data.rows;
        this.total = res.data.total;
      });
    },
    /** 页面大小改变事件 */
    handlePageSize(newPageSize) {
      this.queryInfo.pageSize = newPageSize;
      this.findPage();
    },
    /** 页面改变事件 */
    handlePageNumber(newPageNumber) {
      // 将分页参数新数据传递给pageNumber(分页参数)
      this.queryInfo.pageNumber = newPageNumber;
      this.findPage();
    },
    /** 格式化性别 */
    formatSex(row) {
      if (row.sex === "1") {
        return "男";
      } else if (row.sex === "2") {
        return "女";
      } else {
        return "不限";
      }
    },
    /** 格式化类型 */
    formatType(row) {
      if (row.type === "1") {
        return "检验";
      } else if (row.type === "2") {
        return "套餐";
      }
    },
    /** 图片预览*/
    preview(src) {
      let arr = [];
      if (src) {
        if (!src.startsWith("https")) {
          src = this.$qiniu + src;
        }
      }
      // arr.push(src.startsWith('https')?src:this.$qiniu+src);
      arr.push(src);
      return arr;
    },
    /** 跳转套餐详情页 */
    findById(id) {
      console.log("传出Id", id);
      this.$router.push({
        path: "/order/stemealList",
        query: { stemealId: id },
      });
    },
  },
};
</script>

<style scoped></style>
